package com.hwadee.ems.service.impl;

import java.util.List;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.po.Exam;
import com.hwadee.ems.domain.vo.ExamVO;
import com.hwadee.ems.mapper.ExamMapper;
import com.hwadee.ems.service.IExamService;
import com.hwadee.ems.service.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * <p>
 * 考试 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IExamService {

    private static IExamService instance;

    public static IExamService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Resource
    private IUserService userService;

    @Override
    public Result<IPage<ExamVO>> page(
        PageQuery pageQuery, 
        String courseName, 
        String classroomName, 
        String className
    ) {
        List<ExamVO> vos = baseMapper.selectPageByConditions(
            null,
            null,
            courseName,
            classroomName,
            className,
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<ExamVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

    @Override
    public Result<IPage<ExamVO>> pageByInvigilatorId(
        PageQuery pageQuery, 
        Long invigilatorId, 
        String courseName,
        String classroomName, 
        String className
    ) {
        List<ExamVO> vos = baseMapper.selectPageByConditions(
            invigilatorId,
            null,
            courseName,
            classroomName,
            className,
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<ExamVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

    @Override
    public Result<IPage<ExamVO>> pageByStudentId(
        PageQuery pageQuery, 
        Long studentId, 
        String courseName,
        String classroomName,
        String className
    ) {
        List<ExamVO> vos = baseMapper.selectPageByConditions(
            null,
            userService.getById(studentId).getClassId(),
            courseName,
            classroomName,
            className,
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<ExamVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

}
